package com.minhua.jijiehao2.mainframe.mine.mineFragment.impl

import com.minhua.jijiehao2.bean.BaseBean
import com.minhua.jijiehao2.bean.LoginBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.http.*
import com.minhua.jijiehao2.mainframe.mine.mineFragment.imp.IMineModel
import com.minhua.jijiehao2.utils.SPUtils

class MineModel : IMineModel {
    override fun getUserInfo(map: ParamsMap, callBack: CallBack<LoginBean>) {
        HttpController<LoginBean>().doHttp(
                ApiClient.getInstance().api.getUserInfo(map.toRequestBody()),
                object : BaseSubscriber<LoginBean>() {
                    override fun dealData(t: BaseBean<LoginBean>) {
                        callBack.onSuccess(t)
                    }
                },
                false
        )
    }

    override fun getPhone(map: ParamsMap, callBack: CallBack<String>) {
        HttpController<String>().doHttp(
                ApiClient.getInstance().api.getPhone(map.toRequestBody()),
                object : BaseSubscriber<String>() {
                    override fun dealData(t: BaseBean<String>) {
                        callBack.onSuccess(t)
                    }
                },
                false
        )
    }

    override fun saveLoginInfo(loginBean: LoginBean) {
        if (loginBean.userId != null) {
            SPUtils.put(DefineKey.USER_ID, loginBean.userId)
        }
        if (loginBean.phone != null) {
            SPUtils.put(DefineKey.USER_NAME, loginBean.phone)
        }
        if (loginBean.realName != null) {
            SPUtils.put(DefineKey.USER_NICK_NAME, loginBean.realName)
        }
        if (loginBean.token != null) {
            SPUtils.put(DefineKey.TOKEN, loginBean.token)
        }
    }
}